package com.example.foobar.controller;

import com.example.foobar.business.service.DaNewCommerCountService;
import com.example.foobar.business.service.ImsPzGuanhuaiTrackService;
import com.example.foobar.entity.DaNewCommerCount;
import com.example.foobar.utils.DateRange;
import lombok.extern.slf4j.Slf4j;
import com.example.foobar.param.DaNewCommerCountPageParam;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 新增用户数记录 控制器
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("/daNewCommerCount")
@Module("daNewCommerCountController")
@Api(value = "新增用户数记录API", tags = {"新增用户数记录"})
public class DaNewCommerCountController extends BaseController {

    @Autowired
    private DaNewCommerCountService daNewCommerCountService;

    @Autowired
    private ImsPzGuanhuaiTrackService imsPzGuanhuaiTrackService;
    /**
     * 添加新增用户数记录
     */
    @PostMapping("/add")
    @OperationLog(name = "添加新增用户数记录", type = OperationLogType.ADD)
    @ApiOperation(value = "添加新增用户数记录", response = ApiResult.class)
    public ApiResult<Boolean> addDaNewCommerCount(@Validated(Add.class) @RequestBody DaNewCommerCount daNewCommerCount) throws Exception {
        boolean flag = daNewCommerCountService.saveDaNewCommerCount(daNewCommerCount);
        return ApiResult.result(flag);
    }

    /**
     * 修改新增用户数记录
     */
    @PostMapping("/update")
    @OperationLog(name = "修改新增用户数记录", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改新增用户数记录", response = ApiResult.class)
    public ApiResult<Boolean> updateDaNewCommerCount(@Validated(Update.class) @RequestBody DaNewCommerCount daNewCommerCount) throws Exception {
        boolean flag = daNewCommerCountService.updateDaNewCommerCount(daNewCommerCount);
        return ApiResult.result(flag);
    }

    /**
     * 修改备注
     */
    @PostMapping("/updateRemark")
    @OperationLog(name = "修改备注", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改备注", response = ApiResult.class)
    public ApiResult<Boolean> updateRemark(int newComerCountId, String remark)  {
        boolean flag = daNewCommerCountService.updateRemark(newComerCountId, remark);
        return ApiResult.result(flag);
    }

    /**
     * 删除新增用户数记录
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除新增用户数记录", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除新增用户数记录", response = ApiResult.class)
    public ApiResult<Boolean> deleteDaNewCommerCount(@PathVariable("id") Long id) throws Exception {
        boolean flag = daNewCommerCountService.deleteDaNewCommerCount(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取新增用户数记录详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "新增用户数记录详情", type = OperationLogType.INFO)
    @ApiOperation(value = "新增用户数记录详情", response = DaNewCommerCount.class)
    public ApiResult<DaNewCommerCount> getDaNewCommerCount(@PathVariable("id") Long id) throws Exception {
        DaNewCommerCount daNewCommerCount = daNewCommerCountService.getById(id);
        return ApiResult.ok(daNewCommerCount);
    }

    /**
     * 新增用户数记录分页列表
     */
    @PostMapping("/getPageList")
    @OperationLog(name = "新增用户数记录分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "新增用户数记录分页列表", response = DaNewCommerCount.class)
    public ApiResult<Paging<DaNewCommerCount>> getDaNewCommerCountPageList(@Validated @RequestBody DaNewCommerCountPageParam daNewCommerCountPageParam) throws Exception {
        Paging<DaNewCommerCount> paging = daNewCommerCountService.getDaNewCommerCountPageList(daNewCommerCountPageParam);
        return ApiResult.ok(paging);
    }

    /**
     * 新增用户数记录
     */
    @PostMapping("/pullNewCommerCountFromWeiqing")
    @OperationLog(name = "更新新增用户", type = OperationLogType.PAGE)
    @ApiOperation(value = "更新新增用户", response = DaNewCommerCount.class)
    public ApiResult<Boolean> pullNewCommerCountFromWeiqing(String uniacid, String startDate, String endDate) throws Exception {
        Date startDateTemp = DateUtils.parseDate(startDate, "YYYY-MM-dd");
        Date endDateTemp = DateUtils.parseDate(endDate, "YYYY-MM-dd");
        DateRange dateRange = new DateRange(startDateTemp, endDateTemp);
        daNewCommerCountService.pullYestdayNewCommerCount(Integer.valueOf(uniacid), dateRange);
        return ApiResult.ok(true);
    }

    /**
     * 新增用户数记录分页列表
     */
    @PostMapping("/reloadNewCommerCountDate")
    @OperationLog(name = "重新刷新新增用户数据", type = OperationLogType.PAGE)
    @ApiOperation(value = "重新刷新新增用户数据", response = DaNewCommerCount.class)
    public void reloadNewCommerCountDate() throws Exception {
        daNewCommerCountService.reloadNewCommerCountDate();
    }
}

